<?php

namespace App\Http\Services;

use App\Http\Model\FinanceRechargeOrder;
use App\Http\Model\PayException;
use App\Http\Model\User;

/**
 * 用户标签
 */
class PayExceptionServices extends CommonServices
{
    private $model_payException;
    private $model_rechargeOrder;
    private $model_user;
    public function __construct()
    {
        parent::__construct();
        $this->model_payException = new PayException();
        $this->model_rechargeOrder = new FinanceRechargeOrder();
        $this->model_user = new User();
    }

//    public function run(){
//        $users = $this->user->getTenMinRegistUsers();
//        $usersk = [];
//        foreach ($users as $user) {
//            $usersk[$user['user_id']] = $user;
//        }
//        $uids = array_column($users, 'user_id');
//        $orders = $this->model_rechargeOrder ->getTenMinPayOrdersByUids($uids);
//        $retData = [];
//        foreach ($orders as $order) {
//            $retData[] = [
//                'user_id' => $order['user_id'],
//                'uuid' => $usersk[$order['user_id']]['uuid'],
//                'nickname' => $usersk[$order['user_id']]['nickname'],
//                'amount' =>  $order['price'],
//                'regist_time' => $usersk[$order['user_id']]['create_time'],
//                'pay_time' => $order['create_time'],
//            ];
//        }
//
//        foreach ($retData as $data) {
//            $this->model_payException->add($data);
//        }
//
//        return true;
//    }

    public function run(){
        $orders = $this->model_rechargeOrder->getTenMinExceptionOrders();
        $ordersK = [];
        $uids = [];
        foreach ($orders as $order) {
            if ($order['total_num'] >= 8){
                $ordersK[$order['user_id']] = $order;
                $uids[] = $order['user_id'];
            }
        }
        $users =$this->model_user->getWithinOnDayRegistUsersByUids($uids);
        foreach ($users as $user) {
            $data = [
                'user_id' => $user['user_id'],
                'uuid' => $user['uuid'],
                'nickname' => $user['nickname'],
                'total_num' =>  $ordersK[$user['user_id']]['total_num'],
                'total_amount' =>  $ordersK[$user['user_id']]['total_amount'],
                'regist_time' => $user['create_time'],
            ];
            $this->model_payException->add($data);
        }
    }

    public function getByUserId($userId){
        return $this->model_payException->getByUserId($userId);
    }

}